package com.duoduo.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.util.Log;

import com.duoduo.bean.NewsDetail;
import com.duoduo.bean.NewsTitle;
import com.duoduo.http.HttpUtil;
import com.duoduo.sanguoshanews.R;

public class WebData {
	public static List<NewsTitle> getNewsFromWeb(Context context)
			throws IOException {
		return getNews2(context);
	}

	private static List<NewsTitle> getNews2(Context context) throws IOException {
		String path = context.getString(R.string.sys_news_url);
		InputStream inputStream = HttpUtil.getInputStream(path);

		BufferedReader bufferedReader;
		StringBuffer stringBuffer = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(
					inputStream, context.getString(R.string.web_encoding)));
			stringBuffer = new StringBuffer();
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				stringBuffer.append(line + "\n");
			}
			inputStream.close();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String pageString = stringBuffer.toString();
		Document doc = Jsoup.parse(pageString);
		Element newsContent = doc.select("#tabsnews-content > div").first();
		Elements news = newsContent.select("a");
		List<NewsTitle> list = new ArrayList<NewsTitle>();
		for (Element e : news) {
			list.add(new NewsTitle(e.attr("href"), e.html()));
		}
		return list;
	}

	public static NewsDetail getNewsDetail(Context context, String path,
			String newsTitle) throws IOException {
		InputStream inputStream = HttpUtil.getInputStream(path);

		BufferedReader bufferedReader;
		StringBuffer stringBuffer = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(
					inputStream, context.getString(R.string.web_encoding)));
			stringBuffer = new StringBuffer();
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				stringBuffer.append(line + "\n");
			}
			inputStream.close();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String pageString = stringBuffer.toString();
		NewsDetail newsDetailObj = new NewsDetail();
		newsDetailObj.setUrl(path);

		Document doc = Jsoup.parse(pageString);

		Element newsDetail = doc.select(".newsDetail").first();

		try {
			Element title = newsDetail.select("h1").first();
			newsDetailObj.setTitle(title.html());

			Element pubDate = newsDetail.select("span.newsFLeft").first();
			newsDetailObj.setPubdate(pubDate.html());
			Element newsText = newsDetail.select("div.newsText").first();
			StringBuffer buffer = new StringBuffer();
			Elements newsFragments = newsText
					.select("span[style^=font-family]");
			String newsFlip;
			for (Element e : newsFragments) {
				newsFlip = e.html().replaceAll("&middot;", "•");
				buffer.append(newsFlip.replaceAll("<[^>]*>|(?:&nbsp;)", ""));
				buffer.append("\n");
			}
			newsDetailObj.setContent(buffer.toString());
		} catch (NullPointerException e) {
			Log.i("web", "这条新闻比较特别");
			newsDetailObj.setTitle(newsTitle);
		}

		return newsDetailObj;
	}
}
